package com.mh.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.mh.entity.BookType;
import com.mh.jdbc.util.StringUtil;
import com.mh.service.BookTypeService;

public class BookTypeAddFrame extends JInternalFrame {
	private BookTypeService bookTypeService= new BookTypeService();
	private JTextField bookTypeName;
	private JTextArea bookTypeDesc;

	/**
	 * Create the frame.
	 * @throws PropertyVetoException 
	 */
	public BookTypeAddFrame(){
		JLabel lblNewLabel = new JLabel("图书类别名称:");
		JLabel lblNewLabel_1 = new JLabel("图书类别描述:");
		bookTypeName = new JTextField();
		bookTypeName.setColumns(10);
		bookTypeDesc = new JTextArea();
		JButton btnNewButton = new JButton("添加");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookTypeAddActionPerformed(e);
			}
		});
		btnNewButton.setIcon(new ImageIcon(BookTypeAddFrame.class.getResource("/com/mh/images/add.png")));
		btnNewButton.setName("bookTypeAdd");
		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetValActionPerformed(e);
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(BookTypeAddFrame.class.getResource("/com/mh/images/reset.png")));
		btnNewButton_1.setName("bookTypeReset");
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(49)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnNewButton)
							.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(btnNewButton_1))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addComponent(lblNewLabel)
								.addComponent(lblNewLabel_1))
							.addGap(18)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(bookTypeDesc)
								.addComponent(bookTypeName, GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE))))
					.addGap(61))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(38)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(bookTypeName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(35)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_1)
						.addComponent(bookTypeDesc, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
					.addGap(30))
		);
		getContentPane().setLayout(groupLayout);
		setTitle("图书类别添加");
		setBounds(100, 100, 420, 300);
		setClosable(true);
		setBounds(100, 100, 400,360);
	}

	protected void bookTypeAddActionPerformed(ActionEvent evt) {
		String bookTypeNameVal = this.bookTypeName.getText();
		String bookTypeDescVal = this.bookTypeDesc.getText();
		if(StringUtil.isEmpty(bookTypeNameVal)){
			JOptionPane.showMessageDialog(null, "图书类型名称不能为空！");
			return;
		}
		if(!bookTypeService.bookNameifCanInsert(bookTypeNameVal)){
			JOptionPane.showMessageDialog(null, "图书类型名称重复！");
			return;
		}
		int sts = bookTypeService.bookTypeAdd(new BookType(bookTypeNameVal, bookTypeDescVal));
		JOptionPane.showMessageDialog(null, sts >0 ? "图书类型添加成功！": "图书类型添加失败！");
	}

	protected void resetValActionPerformed(ActionEvent evt) {
		this.bookTypeName.setText("");
		this.bookTypeDesc.setText("");
	}
}
